package P6.T06;

import javax.swing.*;
import java.awt.event.*;
import java.util.Random;

public class JMyFrame extends JFrame {
    public JMyFrame() {
        this.setTitle("菜单示例");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLayout(null);
        this.setSize(500, 500);
        this.setLocationRelativeTo(null);
        JPopupMenu popup = new JPopupMenu();

        JMenuBar menuBar = new JMenuBar();
        this.setJMenuBar(menuBar);
        JMenu m1=new JMenu("文件");
        JMenu m2=new JMenu("编辑");
        JMenu m3=new JMenu("帮助");
        menuBar.add(m1);
        menuBar.add(m2);
        menuBar.add(m3);
        JMenu m11=new JMenu("新建");
        JMenuItem m12=new JMenuItem("添加走马灯");
        JMenuItem m13=new JMenuItem("退出");
        m1.add(m11);
        m1.add(m12);
        m1.add(m13);
        m12.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JLabel lbl=new JLabel();
                Random r=new Random();
                lbl.setBounds(r.nextInt(300), r.nextInt(300), 65, 20);
                lbl.setVisible(true);
                add(lbl);
                Thread t=new Thread(new Runnable() {
                    private String s="新年快乐     ";
                    @Override
                    public void run() {

                        while(true){
                            s=s.substring(1,s.length())+s.substring(0,1);
                            lbl.setText(s);

                            try {
                                Thread.sleep(600);
                            } catch (InterruptedException ex) {
                                throw new RuntimeException(ex);
                            }
                        }
                    }
                });
                t.start();
            }
        });
        m13.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        popup.add(m1);
        popup.add(m2);
        popup.add(m3);

        this.getContentPane().addMouseListener(
                new MouseListener() {
                    @Override
                    public void mouseClicked(MouseEvent e) {

                    }

                    @Override
                    public void mousePressed(MouseEvent e) {

                    }

                    @Override
                    public void mouseReleased(MouseEvent e) {
                        if(e.getButton()==MouseEvent.BUTTON3){
                            popup.show(e.getComponent(), e.getX(), e.getY());
                        }
                    }

                    @Override
                    public void mouseEntered(MouseEvent e) {

                    }

                    @Override
                    public void mouseExited(MouseEvent e) {

                    }
                }
        );

        this.setVisible(true);
    }
}
